

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  
  <title>mindspore.nn.layer.image &mdash; MindSpore master documentation</title>
  

  
  <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />

  
  

  
  

  

  
  <!--[if lt IE 9]>
    <script src="../../../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
        <script src="../../../../_static/jquery.js"></script>
        <script src="../../../../_static/underscore.js"></script>
        <script src="../../../../_static/doctools.js"></script>
        <script src="../../../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../../../_static/js/theme.js"></script>

    
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../../index.html" class="icon icon-home"> MindSpore
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">MindSpore Python API</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.html">mindspore</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.common.initializer.html">mindspore.common.initializer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.communication.html">mindspore.communication</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.compression.html">mindspore.compression</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.context.html">mindspore.context</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.html">mindspore.dataset</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.audio.html">mindspore.dataset.audio</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.config.html">mindspore.dataset.config</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.text.html">mindspore.dataset.text</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.transforms.html">mindspore.dataset.transforms</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.vision.html">mindspore.dataset.vision</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.mindrecord.html">mindspore.mindrecord</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.nn.html">mindspore.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.nn.probability.html">mindspore.nn.probability</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.nn.transformer.html">mindspore.nn.transformer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.numpy.html">mindspore.numpy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.ops.html">mindspore.ops</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.parallel.html">mindspore.parallel</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.parallel.nn.html">mindspore.parallel.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.profiler.html">mindspore.profiler</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.scipy.html">mindspore.scipy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.train.html">mindspore.train</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.boost.html">mindspore.boost</a></li>
</ul>
<p class="caption"><span class="caption-text">MindSpore C++ API</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://www.mindspore.cn/lite/api/zh-CN/master/api_cpp/mindspore.html">MindSpore Lite↗</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../index.html">MindSpore</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          

















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../index.html" class="icon icon-home"></a> &raquo;</li>
        
          <li><a href="../../../index.html">Module code</a> &raquo;</li>
        
      <li>mindspore.nn.layer.image</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for mindspore.nn.layer.image</h1><div class="highlight"><pre>
<span></span><span class="c1"># Copyright 2020-2021 Huawei Technologies Co., Ltd</span>
<span class="c1">#</span>
<span class="c1"># Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span>
<span class="c1"># you may not use this file except in compliance with the License.</span>
<span class="c1"># You may obtain a copy of the License at</span>
<span class="c1">#</span>
<span class="c1"># http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c1">#</span>
<span class="c1"># Unless required by applicable law or agreed to in writing, software</span>
<span class="c1"># distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span>
<span class="c1"># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="c1"># See the License for the specific language governing permissions and</span>
<span class="c1"># limitations under the License.</span>
<span class="c1"># ============================================================================</span>
<span class="sd">&quot;&quot;&quot;image&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">numbers</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">mindspore.common.dtype</span> <span class="k">as</span> <span class="nn">mstype</span>
<span class="kn">from</span> <span class="nn">mindspore.common.tensor</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="kn">from</span> <span class="nn">mindspore.ops</span> <span class="kn">import</span> <span class="n">operations</span> <span class="k">as</span> <span class="n">P</span>
<span class="kn">from</span> <span class="nn">mindspore.ops</span> <span class="kn">import</span> <span class="n">functional</span> <span class="k">as</span> <span class="n">F</span>
<span class="kn">from</span> <span class="nn">mindspore.ops.primitive</span> <span class="kn">import</span> <span class="n">constexpr</span>
<span class="kn">from</span> <span class="nn">mindspore._checkparam</span> <span class="kn">import</span> <span class="n">Rel</span><span class="p">,</span> <span class="n">Validator</span> <span class="k">as</span> <span class="n">validator</span>
<span class="kn">from</span> <span class="nn">.conv</span> <span class="kn">import</span> <span class="n">Conv2d</span>
<span class="kn">from</span> <span class="nn">.container</span> <span class="kn">import</span> <span class="n">CellList</span>
<span class="kn">from</span> <span class="nn">.pooling</span> <span class="kn">import</span> <span class="n">AvgPool2d</span>
<span class="kn">from</span> <span class="nn">.activation</span> <span class="kn">import</span> <span class="n">ReLU</span>
<span class="kn">from</span> <span class="nn">..cell</span> <span class="kn">import</span> <span class="n">Cell</span>

<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;ImageGradients&#39;</span><span class="p">,</span> <span class="s1">&#39;SSIM&#39;</span><span class="p">,</span> <span class="s1">&#39;MSSSIM&#39;</span><span class="p">,</span> <span class="s1">&#39;PSNR&#39;</span><span class="p">,</span> <span class="s1">&#39;CentralCrop&#39;</span><span class="p">]</span>


<div class="viewcode-block" id="ImageGradients"><a class="viewcode-back" href="../../../../api_python/nn/mindspore.nn.ImageGradients.html#mindspore.nn.ImageGradients">[docs]</a><span class="k">class</span> <span class="nc">ImageGradients</span><span class="p">(</span><span class="n">Cell</span><span class="p">):</span>
    <span class="sa">r</span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Returns two tensors, the first is along the height dimension and the second is along the width dimension.</span>

<span class="sd">    Assume an image shape is :math:`h*w`. The gradients along the height and the width are :math:`dy` and :math:`dx`,</span>
<span class="sd">    respectively.</span>

<span class="sd">    .. math::</span>
<span class="sd">        dy[i] = \begin{cases} image[i+1, :]-image[i, :], &amp;if\ 0&lt;=i&lt;h-1 \cr</span>
<span class="sd">        0, &amp;if\ i==h-1\end{cases}</span>

<span class="sd">        dx[i] = \begin{cases} image[:, i+1]-image[:, i], &amp;if\ 0&lt;=i&lt;w-1 \cr</span>
<span class="sd">        0, &amp;if\ i==w-1\end{cases}</span>

<span class="sd">    Inputs:</span>
<span class="sd">        - **images** (Tensor) - The input image data, with format &#39;NCHW&#39;.</span>

<span class="sd">    Outputs:</span>
<span class="sd">        - **dy** (Tensor) - vertical image gradients, the same type and shape as input.</span>
<span class="sd">        - **dx** (Tensor) - horizontal image gradients, the same type and shape as input.</span>

<span class="sd">    Raises:</span>
<span class="sd">        ValueError: If length of shape of `images` is not equal to 4.</span>

<span class="sd">    Supported Platforms:</span>
<span class="sd">        ``Ascend`` ``GPU`` ``CPU``</span>

<span class="sd">    Examples:</span>
<span class="sd">        &gt;&gt;&gt; net = nn.ImageGradients()</span>
<span class="sd">        &gt;&gt;&gt; image = Tensor(np.array([[[[1, 2], [3, 4]]]]), dtype=mindspore.int32)</span>
<span class="sd">        &gt;&gt;&gt; output = net(image)</span>
<span class="sd">        &gt;&gt;&gt; print(output)</span>
<span class="sd">        (Tensor(shape=[1, 1, 2, 2], dtype=Int32, value=</span>
<span class="sd">        [[[[2, 2],</span>
<span class="sd">           [0, 0]]]]), Tensor(shape=[1, 1, 2, 2], dtype=Int32, value=</span>
<span class="sd">        [[[[1, 0],</span>
<span class="sd">           [1, 0]]]]))</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">ImageGradients</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">construct</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">images</span><span class="p">):</span>
        <span class="n">check</span> <span class="o">=</span> <span class="n">_check_input_4d</span><span class="p">(</span><span class="n">F</span><span class="o">.</span><span class="n">shape</span><span class="p">(</span><span class="n">images</span><span class="p">),</span> <span class="s2">&quot;images&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="p">)</span>
        <span class="n">images</span> <span class="o">=</span> <span class="n">F</span><span class="o">.</span><span class="n">depend</span><span class="p">(</span><span class="n">images</span><span class="p">,</span> <span class="n">check</span><span class="p">)</span>
        <span class="n">batch_size</span><span class="p">,</span> <span class="n">depth</span><span class="p">,</span> <span class="n">height</span><span class="p">,</span> <span class="n">width</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Shape</span><span class="p">()(</span><span class="n">images</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">height</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">dy</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Fill</span><span class="p">()(</span><span class="n">P</span><span class="o">.</span><span class="n">DType</span><span class="p">()(</span><span class="n">images</span><span class="p">),</span> <span class="p">(</span><span class="n">batch_size</span><span class="p">,</span> <span class="n">depth</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">width</span><span class="p">),</span> <span class="mi">0</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">dy</span> <span class="o">=</span> <span class="n">images</span><span class="p">[:,</span> <span class="p">:,</span> <span class="mi">1</span><span class="p">:,</span> <span class="p">:]</span> <span class="o">-</span> <span class="n">images</span><span class="p">[:,</span> <span class="p">:,</span> <span class="p">:</span><span class="n">height</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="p">:]</span>
            <span class="n">dy_last</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Fill</span><span class="p">()(</span><span class="n">P</span><span class="o">.</span><span class="n">DType</span><span class="p">()(</span><span class="n">images</span><span class="p">),</span> <span class="p">(</span><span class="n">batch_size</span><span class="p">,</span> <span class="n">depth</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">width</span><span class="p">),</span> <span class="mi">0</span><span class="p">)</span>
            <span class="n">dy</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Concat</span><span class="p">(</span><span class="mi">2</span><span class="p">)((</span><span class="n">dy</span><span class="p">,</span> <span class="n">dy_last</span><span class="p">))</span>

        <span class="k">if</span> <span class="n">width</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">dx</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Fill</span><span class="p">()(</span><span class="n">P</span><span class="o">.</span><span class="n">DType</span><span class="p">()(</span><span class="n">images</span><span class="p">),</span> <span class="p">(</span><span class="n">batch_size</span><span class="p">,</span> <span class="n">depth</span><span class="p">,</span> <span class="n">height</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="mi">0</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">dx</span> <span class="o">=</span> <span class="n">images</span><span class="p">[:,</span> <span class="p">:,</span> <span class="p">:,</span> <span class="mi">1</span><span class="p">:]</span> <span class="o">-</span> <span class="n">images</span><span class="p">[:,</span> <span class="p">:,</span> <span class="p">:,</span> <span class="p">:</span><span class="n">width</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span>
            <span class="n">dx_last</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Fill</span><span class="p">()(</span><span class="n">P</span><span class="o">.</span><span class="n">DType</span><span class="p">()(</span><span class="n">images</span><span class="p">),</span> <span class="p">(</span><span class="n">batch_size</span><span class="p">,</span> <span class="n">depth</span><span class="p">,</span> <span class="n">height</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="mi">0</span><span class="p">)</span>
            <span class="n">dx</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Concat</span><span class="p">(</span><span class="mi">3</span><span class="p">)((</span><span class="n">dx</span><span class="p">,</span> <span class="n">dx_last</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">dy</span><span class="p">,</span> <span class="n">dx</span></div>


<span class="k">def</span> <span class="nf">_convert_img_dtype_to_float32</span><span class="p">(</span><span class="n">img</span><span class="p">,</span> <span class="n">max_val</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;convert img dtype to float32&quot;&quot;&quot;</span>
    <span class="c1"># Usually max_val is 1.0 or 255, we will do the scaling if max_val &gt; 1.</span>
    <span class="c1"># We will scale img pixel value if max_val &gt; 1. and just cast otherwise.</span>
    <span class="n">ret</span> <span class="o">=</span> <span class="n">F</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="n">img</span><span class="p">,</span> <span class="n">mstype</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
    <span class="n">max_val</span> <span class="o">=</span> <span class="n">F</span><span class="o">.</span><span class="n">scalar_cast</span><span class="p">(</span><span class="n">max_val</span><span class="p">,</span> <span class="n">mstype</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">max_val</span> <span class="o">&gt;</span> <span class="mf">1.</span><span class="p">:</span>
        <span class="n">scale</span> <span class="o">=</span> <span class="mf">1.</span> <span class="o">/</span> <span class="n">max_val</span>
        <span class="n">ret</span> <span class="o">=</span> <span class="n">ret</span> <span class="o">*</span> <span class="n">scale</span>
    <span class="k">return</span> <span class="n">ret</span>


<span class="nd">@constexpr</span>
<span class="k">def</span> <span class="nf">_get_dtype_max</span><span class="p">(</span><span class="n">dtype</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;get max of the dtype&quot;&quot;&quot;</span>
    <span class="n">np_type</span> <span class="o">=</span> <span class="n">mstype</span><span class="o">.</span><span class="n">dtype_to_nptype</span><span class="p">(</span><span class="n">dtype</span><span class="p">)</span>
    <span class="k">if</span> <span class="nb">issubclass</span><span class="p">(</span><span class="n">np_type</span><span class="p">,</span> <span class="n">numbers</span><span class="o">.</span><span class="n">Integral</span><span class="p">):</span>
        <span class="n">dtype_max</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">float64</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">iinfo</span><span class="p">(</span><span class="n">np_type</span><span class="p">)</span><span class="o">.</span><span class="n">max</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">dtype_max</span> <span class="o">=</span> <span class="mf">1.0</span>
    <span class="k">return</span> <span class="n">dtype_max</span>


<span class="nd">@constexpr</span>
<span class="k">def</span> <span class="nf">_check_input_4d</span><span class="p">(</span><span class="n">input_shape</span><span class="p">,</span> <span class="n">param_name</span><span class="p">,</span> <span class="n">func_name</span><span class="p">):</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">input_shape</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">4</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;For &#39;</span><span class="si">{</span><span class="n">func_name</span><span class="si">}</span><span class="s2">&#39;, the dimension of &#39;</span><span class="si">{</span><span class="n">param_name</span><span class="si">}</span><span class="s2">&#39; should be 4d, &quot;</span>
                         <span class="sa">f</span><span class="s2">&quot;but got </span><span class="si">{</span><span class="nb">len</span><span class="p">(</span><span class="n">input_shape</span><span class="p">)</span><span class="si">}</span><span class="s2">.&quot;</span><span class="p">)</span>
    <span class="k">return</span> <span class="kc">True</span>


<span class="nd">@constexpr</span>
<span class="k">def</span> <span class="nf">_check_input_filter_size</span><span class="p">(</span><span class="n">input_shape</span><span class="p">,</span> <span class="n">param_name</span><span class="p">,</span> <span class="n">filter_size</span><span class="p">,</span> <span class="n">func_name</span><span class="p">):</span>
    <span class="n">_check_input_4d</span><span class="p">(</span><span class="n">input_shape</span><span class="p">,</span> <span class="n">param_name</span><span class="p">,</span> <span class="n">func_name</span><span class="p">)</span>
    <span class="n">validator</span><span class="o">.</span><span class="n">check</span><span class="p">(</span><span class="n">param_name</span> <span class="o">+</span> <span class="s2">&quot; shape[2]&quot;</span><span class="p">,</span> <span class="n">input_shape</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="s2">&quot;filter_size&quot;</span><span class="p">,</span> <span class="n">filter_size</span><span class="p">,</span> <span class="n">Rel</span><span class="o">.</span><span class="n">GE</span><span class="p">,</span> <span class="n">func_name</span><span class="p">)</span>
    <span class="n">validator</span><span class="o">.</span><span class="n">check</span><span class="p">(</span><span class="n">param_name</span> <span class="o">+</span> <span class="s2">&quot; shape[3]&quot;</span><span class="p">,</span> <span class="n">input_shape</span><span class="p">[</span><span class="mi">3</span><span class="p">],</span> <span class="s2">&quot;filter_size&quot;</span><span class="p">,</span> <span class="n">filter_size</span><span class="p">,</span> <span class="n">Rel</span><span class="o">.</span><span class="n">GE</span><span class="p">,</span> <span class="n">func_name</span><span class="p">)</span>


<span class="nd">@constexpr</span>
<span class="k">def</span> <span class="nf">_check_input_dtype</span><span class="p">(</span><span class="n">input_dtype</span><span class="p">,</span> <span class="n">param_name</span><span class="p">,</span> <span class="n">allow_dtypes</span><span class="p">,</span> <span class="n">cls_name</span><span class="p">):</span>
    <span class="n">validator</span><span class="o">.</span><span class="n">check_type_name</span><span class="p">(</span><span class="n">param_name</span><span class="p">,</span> <span class="n">input_dtype</span><span class="p">,</span> <span class="n">allow_dtypes</span><span class="p">,</span> <span class="n">cls_name</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">_conv2d</span><span class="p">(</span><span class="n">in_channels</span><span class="p">,</span> <span class="n">out_channels</span><span class="p">,</span> <span class="n">kernel_size</span><span class="p">,</span> <span class="n">weight</span><span class="p">,</span> <span class="n">stride</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">padding</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">Conv2d</span><span class="p">(</span><span class="n">in_channels</span><span class="p">,</span> <span class="n">out_channels</span><span class="p">,</span> <span class="n">kernel_size</span><span class="o">=</span><span class="n">kernel_size</span><span class="p">,</span> <span class="n">stride</span><span class="o">=</span><span class="n">stride</span><span class="p">,</span>
                  <span class="n">weight_init</span><span class="o">=</span><span class="n">weight</span><span class="p">,</span> <span class="n">padding</span><span class="o">=</span><span class="n">padding</span><span class="p">,</span> <span class="n">pad_mode</span><span class="o">=</span><span class="s2">&quot;valid&quot;</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">_create_window</span><span class="p">(</span><span class="n">size</span><span class="p">,</span> <span class="n">sigma</span><span class="p">):</span>
    <span class="n">x_data</span><span class="p">,</span> <span class="n">y_data</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mgrid</span><span class="p">[</span><span class="o">-</span><span class="n">size</span> <span class="o">//</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span><span class="p">:</span><span class="n">size</span> <span class="o">//</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="n">size</span> <span class="o">//</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span><span class="p">:</span><span class="n">size</span> <span class="o">//</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span>
    <span class="n">x_data</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">expand_dims</span><span class="p">(</span><span class="n">x_data</span><span class="p">,</span> <span class="n">axis</span><span class="o">=-</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
    <span class="n">x_data</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">expand_dims</span><span class="p">(</span><span class="n">x_data</span><span class="p">,</span> <span class="n">axis</span><span class="o">=-</span><span class="mi">1</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span>
    <span class="n">y_data</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">expand_dims</span><span class="p">(</span><span class="n">y_data</span><span class="p">,</span> <span class="n">axis</span><span class="o">=-</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
    <span class="n">y_data</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">expand_dims</span><span class="p">(</span><span class="n">y_data</span><span class="p">,</span> <span class="n">axis</span><span class="o">=-</span><span class="mi">1</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span>
    <span class="n">sigma</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">sigma</span> <span class="o">**</span> <span class="mi">2</span>
    <span class="n">g</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="p">(</span><span class="n">x_data</span> <span class="o">+</span> <span class="n">y_data</span><span class="p">)</span> <span class="o">/</span> <span class="n">sigma</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">transpose</span><span class="p">(</span><span class="n">g</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">g</span><span class="p">),</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>


<span class="k">def</span> <span class="nf">_split_img</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
    <span class="n">_</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="n">F</span><span class="o">.</span><span class="n">shape</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
    <span class="n">img_split</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Split</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">c</span><span class="p">)</span>
    <span class="n">output</span> <span class="o">=</span> <span class="n">img_split</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">output</span><span class="p">,</span> <span class="n">c</span>


<span class="k">def</span> <span class="nf">_compute_per_channel_loss</span><span class="p">(</span><span class="n">c1</span><span class="p">,</span> <span class="n">c2</span><span class="p">,</span> <span class="n">img1</span><span class="p">,</span> <span class="n">img2</span><span class="p">,</span> <span class="n">conv</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;computes ssim index between img1 and img2 per single channel&quot;&quot;&quot;</span>
    <span class="n">dot_img</span> <span class="o">=</span> <span class="n">img1</span> <span class="o">*</span> <span class="n">img2</span>
    <span class="n">mu1</span> <span class="o">=</span> <span class="n">conv</span><span class="p">(</span><span class="n">img1</span><span class="p">)</span>
    <span class="n">mu2</span> <span class="o">=</span> <span class="n">conv</span><span class="p">(</span><span class="n">img2</span><span class="p">)</span>
    <span class="n">mu1_sq</span> <span class="o">=</span> <span class="n">mu1</span> <span class="o">*</span> <span class="n">mu1</span>
    <span class="n">mu2_sq</span> <span class="o">=</span> <span class="n">mu2</span> <span class="o">*</span> <span class="n">mu2</span>
    <span class="n">mu1_mu2</span> <span class="o">=</span> <span class="n">mu1</span> <span class="o">*</span> <span class="n">mu2</span>
    <span class="n">sigma1_tmp</span> <span class="o">=</span> <span class="n">conv</span><span class="p">(</span><span class="n">img1</span> <span class="o">*</span> <span class="n">img1</span><span class="p">)</span>
    <span class="n">sigma1_sq</span> <span class="o">=</span> <span class="n">sigma1_tmp</span> <span class="o">-</span> <span class="n">mu1_sq</span>
    <span class="n">sigma2_tmp</span> <span class="o">=</span> <span class="n">conv</span><span class="p">(</span><span class="n">img2</span> <span class="o">*</span> <span class="n">img2</span><span class="p">)</span>
    <span class="n">sigma2_sq</span> <span class="o">=</span> <span class="n">sigma2_tmp</span> <span class="o">-</span> <span class="n">mu2_sq</span>
    <span class="n">sigma12_tmp</span> <span class="o">=</span> <span class="n">conv</span><span class="p">(</span><span class="n">dot_img</span><span class="p">)</span>
    <span class="n">sigma12</span> <span class="o">=</span> <span class="n">sigma12_tmp</span> <span class="o">-</span> <span class="n">mu1_mu2</span>
    <span class="n">a</span> <span class="o">=</span> <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">mu1_mu2</span> <span class="o">+</span> <span class="n">c1</span><span class="p">)</span>
    <span class="n">b</span> <span class="o">=</span> <span class="p">(</span><span class="n">mu1_sq</span> <span class="o">+</span> <span class="n">mu2_sq</span> <span class="o">+</span> <span class="n">c1</span><span class="p">)</span>
    <span class="n">v1</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">sigma12</span> <span class="o">+</span> <span class="n">c2</span>
    <span class="n">v2</span> <span class="o">=</span> <span class="n">sigma1_sq</span> <span class="o">+</span> <span class="n">sigma2_sq</span> <span class="o">+</span> <span class="n">c2</span>
    <span class="n">ssim</span> <span class="o">=</span> <span class="p">(</span><span class="n">a</span> <span class="o">*</span> <span class="n">v1</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">b</span> <span class="o">*</span> <span class="n">v2</span><span class="p">)</span>
    <span class="n">cs</span> <span class="o">=</span> <span class="n">v1</span> <span class="o">/</span> <span class="n">v2</span>
    <span class="k">return</span> <span class="n">ssim</span><span class="p">,</span> <span class="n">cs</span>


<span class="k">def</span> <span class="nf">_compute_multi_channel_loss</span><span class="p">(</span><span class="n">c1</span><span class="p">,</span> <span class="n">c2</span><span class="p">,</span> <span class="n">img1</span><span class="p">,</span> <span class="n">img2</span><span class="p">,</span> <span class="n">conv</span><span class="p">,</span> <span class="n">concat</span><span class="p">,</span> <span class="n">mean</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;computes ssim index between img1 and img2 per color channel&quot;&quot;&quot;</span>
    <span class="n">split_img1</span><span class="p">,</span> <span class="n">c</span> <span class="o">=</span> <span class="n">_split_img</span><span class="p">(</span><span class="n">img1</span><span class="p">)</span>
    <span class="n">split_img2</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="n">_split_img</span><span class="p">(</span><span class="n">img2</span><span class="p">)</span>
    <span class="n">multi_ssim</span> <span class="o">=</span> <span class="p">()</span>
    <span class="n">multi_cs</span> <span class="o">=</span> <span class="p">()</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">c</span><span class="p">):</span>
        <span class="n">ssim_per_channel</span><span class="p">,</span> <span class="n">cs_per_channel</span> <span class="o">=</span> <span class="n">_compute_per_channel_loss</span><span class="p">(</span><span class="n">c1</span><span class="p">,</span> <span class="n">c2</span><span class="p">,</span> <span class="n">split_img1</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">split_img2</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">conv</span><span class="p">)</span>
        <span class="n">multi_ssim</span> <span class="o">+=</span> <span class="p">(</span><span class="n">ssim_per_channel</span><span class="p">,)</span>
        <span class="n">multi_cs</span> <span class="o">+=</span> <span class="p">(</span><span class="n">cs_per_channel</span><span class="p">,)</span>

    <span class="n">multi_ssim</span> <span class="o">=</span> <span class="n">concat</span><span class="p">(</span><span class="n">multi_ssim</span><span class="p">)</span>
    <span class="n">multi_cs</span> <span class="o">=</span> <span class="n">concat</span><span class="p">(</span><span class="n">multi_cs</span><span class="p">)</span>

    <span class="n">ssim</span> <span class="o">=</span> <span class="n">mean</span><span class="p">(</span><span class="n">multi_ssim</span><span class="p">,</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span>
    <span class="n">cs</span> <span class="o">=</span> <span class="n">mean</span><span class="p">(</span><span class="n">multi_cs</span><span class="p">,</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span>
    <span class="k">return</span> <span class="n">ssim</span><span class="p">,</span> <span class="n">cs</span>


<div class="viewcode-block" id="SSIM"><a class="viewcode-back" href="../../../../api_python/nn/mindspore.nn.SSIM.html#mindspore.nn.SSIM">[docs]</a><span class="k">class</span> <span class="nc">SSIM</span><span class="p">(</span><span class="n">Cell</span><span class="p">):</span>
    <span class="sa">r</span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Returns SSIM index between two images.</span>

<span class="sd">    Its implementation is based on Wang, Z., Bovik, A. C., Sheikh, H. R., &amp; Simoncelli, E. P. (2004). `Image quality</span>
<span class="sd">    assessment: from error visibility to structural similarity &lt;https://ieeexplore.ieee.org/document/1284395&gt;`_.</span>
<span class="sd">    IEEE transactions on image processing.</span>

<span class="sd">    SSIM is a measure of the similarity of two pictures.</span>
<span class="sd">    Like PSNR, SSIM is often used as an evaluation of image quality. SSIM is a number between 0 and 1.The larger it is,</span>
<span class="sd">    the smaller the gap between the output image and the undistorted image, that is, the better the image quality.</span>
<span class="sd">    When the two images are exactly the same, SSIM=1.</span>

<span class="sd">    .. math::</span>

<span class="sd">        l(x,y)&amp;=\frac{2\mu_x\mu_y+C_1}{\mu_x^2+\mu_y^2+C_1}, C_1=(K_1L)^2.\\</span>
<span class="sd">        c(x,y)&amp;=\frac{2\sigma_x\sigma_y+C_2}{\sigma_x^2+\sigma_y^2+C_2}, C_2=(K_2L)^2.\\</span>
<span class="sd">        s(x,y)&amp;=\frac{\sigma_{xy}+C_3}{\sigma_x\sigma_y+C_3}, C_3=C_2/2.\\</span>
<span class="sd">        SSIM(x,y)&amp;=l*c*s\\&amp;=\frac{(2\mu_x\mu_y+C_1)(2\sigma_{xy}+C_2}{(\mu_x^2+\mu_y^2+C_1)(\sigma_x^2+\sigma_y^2+C_2)}.</span>

<span class="sd">    Args:</span>
<span class="sd">        max_val (Union[int, float]): The dynamic range of the pixel values (255 for 8-bit grayscale images).</span>
<span class="sd">          Default: 1.0.</span>
<span class="sd">        filter_size (int): The size of the Gaussian filter. Default: 11. The value must be greater than or equal to 1.</span>
<span class="sd">        filter_sigma (float): The standard deviation of Gaussian kernel. Default: 1.5.</span>
<span class="sd">          The value must be greater than 0.</span>
<span class="sd">        k1 (float): The constant used to generate c1 in the luminance comparison function. Default: 0.01.</span>
<span class="sd">        k2 (float): The constant used to generate c2 in the contrast comparison function. Default: 0.03.</span>

<span class="sd">    Inputs:</span>
<span class="sd">        - **img1** (Tensor) - The first image batch with format &#39;NCHW&#39;. It must be the same shape and dtype as img2.</span>
<span class="sd">        - **img2** (Tensor) - The second image batch with format &#39;NCHW&#39;. It must be the same shape and dtype as img1.</span>

<span class="sd">    Outputs:</span>
<span class="sd">        Tensor, has the same dtype as img1. It is a 1-D tensor with shape N, where N is the batch num of img1.</span>

<span class="sd">    Raises:</span>
<span class="sd">        TypeError: If `max_val` is neither int nor float.</span>
<span class="sd">        TypeError: If `k1`, `k2` or `filter_sigma` is not a float.</span>
<span class="sd">        TypeError: If `filter_size` is not an int.</span>
<span class="sd">        ValueError: If `max_val` or `filter_sigma` is less than or equal to 0.</span>
<span class="sd">        ValueError: If `filter_size` is less than 0.</span>

<span class="sd">    Supported Platforms:</span>
<span class="sd">        ``Ascend`` ``GPU`` ``CPU``</span>

<span class="sd">    Examples:</span>
<span class="sd">        &gt;&gt;&gt; import numpy as np</span>
<span class="sd">        &gt;&gt;&gt; import mindspore.nn as nn</span>
<span class="sd">        &gt;&gt;&gt; from mindspore import Tensor</span>
<span class="sd">        &gt;&gt;&gt; net = nn.SSIM()</span>
<span class="sd">        &gt;&gt;&gt; img1 = Tensor(np.ones([1, 3, 16, 16]).astype(np.float32))</span>
<span class="sd">        &gt;&gt;&gt; img2 = Tensor(np.ones([1, 3, 16, 16]).astype(np.float32))</span>
<span class="sd">        &gt;&gt;&gt; output = net(img1, img2)</span>
<span class="sd">        &gt;&gt;&gt; print(output)</span>
<span class="sd">        [1.]</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">max_val</span><span class="o">=</span><span class="mf">1.0</span><span class="p">,</span> <span class="n">filter_size</span><span class="o">=</span><span class="mi">11</span><span class="p">,</span> <span class="n">filter_sigma</span><span class="o">=</span><span class="mf">1.5</span><span class="p">,</span> <span class="n">k1</span><span class="o">=</span><span class="mf">0.01</span><span class="p">,</span> <span class="n">k2</span><span class="o">=</span><span class="mf">0.03</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">SSIM</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="n">validator</span><span class="o">.</span><span class="n">check_value_type</span><span class="p">(</span><span class="s1">&#39;max_val&#39;</span><span class="p">,</span> <span class="n">max_val</span><span class="p">,</span> <span class="p">[</span><span class="nb">int</span><span class="p">,</span> <span class="nb">float</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="p">)</span>
        <span class="n">validator</span><span class="o">.</span><span class="n">check_number</span><span class="p">(</span><span class="s1">&#39;max_val&#39;</span><span class="p">,</span> <span class="n">max_val</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="n">Rel</span><span class="o">.</span><span class="n">GT</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">max_val</span> <span class="o">=</span> <span class="n">max_val</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">filter_size</span> <span class="o">=</span> <span class="n">validator</span><span class="o">.</span><span class="n">check_int</span><span class="p">(</span><span class="n">filter_size</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">Rel</span><span class="o">.</span><span class="n">GE</span><span class="p">,</span> <span class="s1">&#39;filter_size&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">filter_sigma</span> <span class="o">=</span> <span class="n">validator</span><span class="o">.</span><span class="n">check_positive_float</span><span class="p">(</span><span class="n">filter_sigma</span><span class="p">,</span> <span class="s1">&#39;filter_sigma&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">k1</span> <span class="o">=</span> <span class="n">validator</span><span class="o">.</span><span class="n">check_value_type</span><span class="p">(</span><span class="s1">&#39;k1&#39;</span><span class="p">,</span> <span class="n">k1</span><span class="p">,</span> <span class="p">[</span><span class="nb">float</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">k2</span> <span class="o">=</span> <span class="n">validator</span><span class="o">.</span><span class="n">check_value_type</span><span class="p">(</span><span class="s1">&#39;k2&#39;</span><span class="p">,</span> <span class="n">k2</span><span class="p">,</span> <span class="p">[</span><span class="nb">float</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="p">)</span>
        <span class="n">window</span> <span class="o">=</span> <span class="n">_create_window</span><span class="p">(</span><span class="n">filter_size</span><span class="p">,</span> <span class="n">filter_sigma</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">conv</span> <span class="o">=</span> <span class="n">_conv2d</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">filter_size</span><span class="p">,</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">window</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">conv</span><span class="o">.</span><span class="n">weight</span><span class="o">.</span><span class="n">requires_grad</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">reduce_mean</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">ReduceMean</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">concat</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Concat</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">construct</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">img1</span><span class="p">,</span> <span class="n">img2</span><span class="p">):</span>
        <span class="n">_check_input_dtype</span><span class="p">(</span><span class="n">F</span><span class="o">.</span><span class="n">dtype</span><span class="p">(</span><span class="n">img1</span><span class="p">),</span> <span class="s2">&quot;img1&quot;</span><span class="p">,</span> <span class="p">[</span><span class="n">mstype</span><span class="o">.</span><span class="n">float32</span><span class="p">,</span> <span class="n">mstype</span><span class="o">.</span><span class="n">float16</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="p">)</span>
        <span class="n">_check_input_filter_size</span><span class="p">(</span><span class="n">F</span><span class="o">.</span><span class="n">shape</span><span class="p">(</span><span class="n">img1</span><span class="p">),</span> <span class="s2">&quot;img1&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">filter_size</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="p">)</span>
        <span class="n">P</span><span class="o">.</span><span class="n">SameTypeShape</span><span class="p">()(</span><span class="n">img1</span><span class="p">,</span> <span class="n">img2</span><span class="p">)</span>
        <span class="n">dtype_max_val</span> <span class="o">=</span> <span class="n">_get_dtype_max</span><span class="p">(</span><span class="n">F</span><span class="o">.</span><span class="n">dtype</span><span class="p">(</span><span class="n">img1</span><span class="p">))</span>
        <span class="n">max_val</span> <span class="o">=</span> <span class="n">F</span><span class="o">.</span><span class="n">scalar_cast</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">max_val</span><span class="p">,</span> <span class="n">F</span><span class="o">.</span><span class="n">dtype</span><span class="p">(</span><span class="n">img1</span><span class="p">))</span>
        <span class="n">max_val</span> <span class="o">=</span> <span class="n">_convert_img_dtype_to_float32</span><span class="p">(</span><span class="n">max_val</span><span class="p">,</span> <span class="n">dtype_max_val</span><span class="p">)</span>
        <span class="n">img1</span> <span class="o">=</span> <span class="n">_convert_img_dtype_to_float32</span><span class="p">(</span><span class="n">img1</span><span class="p">,</span> <span class="n">dtype_max_val</span><span class="p">)</span>
        <span class="n">img2</span> <span class="o">=</span> <span class="n">_convert_img_dtype_to_float32</span><span class="p">(</span><span class="n">img2</span><span class="p">,</span> <span class="n">dtype_max_val</span><span class="p">)</span>

        <span class="n">c1</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">k1</span> <span class="o">*</span> <span class="n">max_val</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span>
        <span class="n">c2</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">k2</span> <span class="o">*</span> <span class="n">max_val</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span>

        <span class="n">ssim_ave_channel</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="n">_compute_multi_channel_loss</span><span class="p">(</span><span class="n">c1</span><span class="p">,</span> <span class="n">c2</span><span class="p">,</span> <span class="n">img1</span><span class="p">,</span> <span class="n">img2</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">conv</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">concat</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">reduce_mean</span><span class="p">)</span>
        <span class="n">loss</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">reduce_mean</span><span class="p">(</span><span class="n">ssim_ave_channel</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">loss</span></div>


<span class="k">def</span> <span class="nf">_downsample</span><span class="p">(</span><span class="n">img1</span><span class="p">,</span> <span class="n">img2</span><span class="p">,</span> <span class="n">op</span><span class="p">):</span>
    <span class="n">a</span> <span class="o">=</span> <span class="n">op</span><span class="p">(</span><span class="n">img1</span><span class="p">)</span>
    <span class="n">b</span> <span class="o">=</span> <span class="n">op</span><span class="p">(</span><span class="n">img2</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span>


<div class="viewcode-block" id="MSSSIM"><a class="viewcode-back" href="../../../../api_python/nn/mindspore.nn.MSSSIM.html#mindspore.nn.MSSSIM">[docs]</a><span class="k">class</span> <span class="nc">MSSSIM</span><span class="p">(</span><span class="n">Cell</span><span class="p">):</span>
    <span class="sa">r</span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Returns MS-SSIM index between two images.</span>

<span class="sd">    Its implementation is based on Wang, Zhou, Eero P. Simoncelli, and Alan C. Bovik. `Multiscale structural similarity</span>
<span class="sd">    for image quality assessment &lt;https://ieeexplore.ieee.org/document/1292216&gt;`_.</span>
<span class="sd">    Signals, Systems and Computers, 2004.</span>

<span class="sd">    .. math::</span>

<span class="sd">        l(x,y)&amp;=\frac{2\mu_x\mu_y+C_1}{\mu_x^2+\mu_y^2+C_1}, C_1=(K_1L)^2.\\</span>
<span class="sd">        c(x,y)&amp;=\frac{2\sigma_x\sigma_y+C_2}{\sigma_x^2+\sigma_y^2+C_2}, C_2=(K_2L)^2.\\</span>
<span class="sd">        s(x,y)&amp;=\frac{\sigma_{xy}+C_3}{\sigma_x\sigma_y+C_3}, C_3=C_2/2.\\</span>
<span class="sd">        MSSSIM(x,y)&amp;=l^\alpha_M*{\prod_{1\leq j\leq M} (c^\beta_j*s^\gamma_j)}.</span>

<span class="sd">    Args:</span>
<span class="sd">        max_val (Union[int, float]): The dynamic range of the pixel values (255 for 8-bit grayscale images).</span>
<span class="sd">          Default: 1.0.</span>
<span class="sd">        power_factors (Union[tuple, list]): Iterable of weights for each scale.</span>
<span class="sd">          Default: (0.0448, 0.2856, 0.3001, 0.2363, 0.1333). Default values obtained by Wang et al.</span>
<span class="sd">        filter_size (int): The size of the Gaussian filter. Default: 11.</span>
<span class="sd">        filter_sigma (float): The standard deviation of Gaussian kernel. Default: 1.5.</span>
<span class="sd">        k1 (float): The constant used to generate c1 in the luminance comparison function. Default: 0.01.</span>
<span class="sd">        k2 (float): The constant used to generate c2 in the contrast comparison function. Default: 0.03.</span>

<span class="sd">    Inputs:</span>
<span class="sd">        - **img1** (Tensor) - The first image batch with format &#39;NCHW&#39;. It must be the same shape and dtype as img2.</span>
<span class="sd">        - **img2** (Tensor) - The second image batch with format &#39;NCHW&#39;. It must be the same shape and dtype as img1.</span>

<span class="sd">    Outputs:</span>
<span class="sd">        Tensor, the value is in range [0, 1]. It is a 1-D tensor with shape N, where N is the batch num of img1.</span>

<span class="sd">    Raises:</span>
<span class="sd">        TypeError: If `max_val` is neither int nor float.</span>
<span class="sd">        TypeError: If `power_factors` is neither tuple nor list.</span>
<span class="sd">        TypeError: If `k1`, `k2` or `filter_sigma` is not a float.</span>
<span class="sd">        TypeError: If `filter_size` is not an int.</span>
<span class="sd">        ValueError: If `max_val` or `filter_sigma` is less than or equal to 0.</span>
<span class="sd">        ValueError: If `filter_size` is less than 0.</span>
<span class="sd">        ValueError: If length of shape of `img1` or `img2` is not equal to 4.</span>

<span class="sd">    Supported Platforms:</span>
<span class="sd">        ``Ascend`` ``GPU``</span>

<span class="sd">    Examples:</span>
<span class="sd">        &gt;&gt;&gt; import numpy as np</span>
<span class="sd">        &gt;&gt;&gt; import mindspore.nn as nn</span>
<span class="sd">        &gt;&gt;&gt; from mindspore import Tensor</span>
<span class="sd">        &gt;&gt;&gt; net = nn.MSSSIM(power_factors=(0.033, 0.033, 0.033))</span>
<span class="sd">        &gt;&gt;&gt; img1 = Tensor(np.ones((1, 3, 128, 128)).astype(np.float32))</span>
<span class="sd">        &gt;&gt;&gt; img2 = Tensor(np.ones((1, 3, 128, 128)).astype(np.float32))</span>
<span class="sd">        &gt;&gt;&gt; output = net(img1, img2)</span>
<span class="sd">        &gt;&gt;&gt; print(output)</span>
<span class="sd">        [1.]</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">max_val</span><span class="o">=</span><span class="mf">1.0</span><span class="p">,</span> <span class="n">power_factors</span><span class="o">=</span><span class="p">(</span><span class="mf">0.0448</span><span class="p">,</span> <span class="mf">0.2856</span><span class="p">,</span> <span class="mf">0.3001</span><span class="p">,</span> <span class="mf">0.2363</span><span class="p">,</span> <span class="mf">0.1333</span><span class="p">),</span> <span class="n">filter_size</span><span class="o">=</span><span class="mi">11</span><span class="p">,</span>
                 <span class="n">filter_sigma</span><span class="o">=</span><span class="mf">1.5</span><span class="p">,</span> <span class="n">k1</span><span class="o">=</span><span class="mf">0.01</span><span class="p">,</span> <span class="n">k2</span><span class="o">=</span><span class="mf">0.03</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">MSSSIM</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="n">validator</span><span class="o">.</span><span class="n">check_value_type</span><span class="p">(</span><span class="s1">&#39;max_val&#39;</span><span class="p">,</span> <span class="n">max_val</span><span class="p">,</span> <span class="p">[</span><span class="nb">int</span><span class="p">,</span> <span class="nb">float</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="p">)</span>
        <span class="n">validator</span><span class="o">.</span><span class="n">check_number</span><span class="p">(</span><span class="s1">&#39;max_val&#39;</span><span class="p">,</span> <span class="n">max_val</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="n">Rel</span><span class="o">.</span><span class="n">GT</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">max_val</span> <span class="o">=</span> <span class="n">max_val</span>
        <span class="n">validator</span><span class="o">.</span><span class="n">check_value_type</span><span class="p">(</span><span class="s1">&#39;power_factors&#39;</span><span class="p">,</span> <span class="n">power_factors</span><span class="p">,</span> <span class="p">[</span><span class="nb">tuple</span><span class="p">,</span> <span class="nb">list</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">filter_size</span> <span class="o">=</span> <span class="n">validator</span><span class="o">.</span><span class="n">check_int</span><span class="p">(</span><span class="n">filter_size</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">Rel</span><span class="o">.</span><span class="n">GE</span><span class="p">,</span> <span class="s1">&#39;filter_size&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">filter_sigma</span> <span class="o">=</span> <span class="n">validator</span><span class="o">.</span><span class="n">check_positive_float</span><span class="p">(</span><span class="n">filter_sigma</span><span class="p">,</span> <span class="s1">&#39;filter_sigma&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">k1</span> <span class="o">=</span> <span class="n">validator</span><span class="o">.</span><span class="n">check_value_type</span><span class="p">(</span><span class="s1">&#39;k1&#39;</span><span class="p">,</span> <span class="n">k1</span><span class="p">,</span> <span class="p">[</span><span class="nb">float</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">k2</span> <span class="o">=</span> <span class="n">validator</span><span class="o">.</span><span class="n">check_value_type</span><span class="p">(</span><span class="s1">&#39;k2&#39;</span><span class="p">,</span> <span class="n">k2</span><span class="p">,</span> <span class="p">[</span><span class="nb">float</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="p">)</span>
        <span class="n">window</span> <span class="o">=</span> <span class="n">_create_window</span><span class="p">(</span><span class="n">filter_size</span><span class="p">,</span> <span class="n">filter_sigma</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">level</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">power_factors</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">conv</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">level</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">conv</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">_conv2d</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">filter_size</span><span class="p">,</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">window</span><span class="p">)))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">conv</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">weight</span><span class="o">.</span><span class="n">requires_grad</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">multi_convs_list</span> <span class="o">=</span> <span class="n">CellList</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">conv</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">weight_tensor</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">power_factors</span><span class="p">,</span> <span class="n">mstype</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">avg_pool</span> <span class="o">=</span> <span class="n">AvgPool2d</span><span class="p">(</span><span class="n">kernel_size</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">stride</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">pad_mode</span><span class="o">=</span><span class="s1">&#39;valid&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">relu</span> <span class="o">=</span> <span class="n">ReLU</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">reduce_mean</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">ReduceMean</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">prod</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">ReduceProd</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pow</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Pow</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">stack</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Stack</span><span class="p">(</span><span class="n">axis</span><span class="o">=-</span><span class="mi">1</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">concat</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Concat</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">construct</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">img1</span><span class="p">,</span> <span class="n">img2</span><span class="p">):</span>
        <span class="n">_check_input_4d</span><span class="p">(</span><span class="n">F</span><span class="o">.</span><span class="n">shape</span><span class="p">(</span><span class="n">img1</span><span class="p">),</span> <span class="s2">&quot;img1&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="p">)</span>
        <span class="n">_check_input_4d</span><span class="p">(</span><span class="n">F</span><span class="o">.</span><span class="n">shape</span><span class="p">(</span><span class="n">img2</span><span class="p">),</span> <span class="s2">&quot;img2&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="p">)</span>
        <span class="n">valid_type</span> <span class="o">=</span> <span class="p">[</span><span class="n">mstype</span><span class="o">.</span><span class="n">float64</span><span class="p">,</span> <span class="n">mstype</span><span class="o">.</span><span class="n">float32</span><span class="p">,</span> <span class="n">mstype</span><span class="o">.</span><span class="n">float16</span><span class="p">,</span> <span class="n">mstype</span><span class="o">.</span><span class="n">uint8</span><span class="p">]</span>
        <span class="n">_check_input_dtype</span><span class="p">(</span><span class="n">F</span><span class="o">.</span><span class="n">dtype</span><span class="p">(</span><span class="n">img1</span><span class="p">),</span> <span class="s1">&#39;img1&#39;</span><span class="p">,</span> <span class="n">valid_type</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="p">)</span>
        <span class="n">P</span><span class="o">.</span><span class="n">SameTypeShape</span><span class="p">()(</span><span class="n">img1</span><span class="p">,</span> <span class="n">img2</span><span class="p">)</span>
        <span class="n">dtype_max_val</span> <span class="o">=</span> <span class="n">_get_dtype_max</span><span class="p">(</span><span class="n">F</span><span class="o">.</span><span class="n">dtype</span><span class="p">(</span><span class="n">img1</span><span class="p">))</span>
        <span class="n">max_val</span> <span class="o">=</span> <span class="n">F</span><span class="o">.</span><span class="n">scalar_cast</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">max_val</span><span class="p">,</span> <span class="n">F</span><span class="o">.</span><span class="n">dtype</span><span class="p">(</span><span class="n">img1</span><span class="p">))</span>
        <span class="n">max_val</span> <span class="o">=</span> <span class="n">_convert_img_dtype_to_float32</span><span class="p">(</span><span class="n">max_val</span><span class="p">,</span> <span class="n">dtype_max_val</span><span class="p">)</span>
        <span class="n">img1</span> <span class="o">=</span> <span class="n">_convert_img_dtype_to_float32</span><span class="p">(</span><span class="n">img1</span><span class="p">,</span> <span class="n">dtype_max_val</span><span class="p">)</span>
        <span class="n">img2</span> <span class="o">=</span> <span class="n">_convert_img_dtype_to_float32</span><span class="p">(</span><span class="n">img2</span><span class="p">,</span> <span class="n">dtype_max_val</span><span class="p">)</span>

        <span class="n">c1</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">k1</span> <span class="o">*</span> <span class="n">max_val</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span>
        <span class="n">c2</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">k2</span> <span class="o">*</span> <span class="n">max_val</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span>

        <span class="n">sim</span> <span class="o">=</span> <span class="p">()</span>
        <span class="n">mcs</span> <span class="o">=</span> <span class="p">()</span>

        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">level</span><span class="p">):</span>
            <span class="n">sim</span><span class="p">,</span> <span class="n">cs</span> <span class="o">=</span> <span class="n">_compute_multi_channel_loss</span><span class="p">(</span><span class="n">c1</span><span class="p">,</span> <span class="n">c2</span><span class="p">,</span> <span class="n">img1</span><span class="p">,</span> <span class="n">img2</span><span class="p">,</span>
                                                  <span class="bp">self</span><span class="o">.</span><span class="n">multi_convs_list</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">concat</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">reduce_mean</span><span class="p">)</span>
            <span class="n">mcs</span> <span class="o">+=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">relu</span><span class="p">(</span><span class="n">cs</span><span class="p">),)</span>
            <span class="n">img1</span><span class="p">,</span> <span class="n">img2</span> <span class="o">=</span> <span class="n">_downsample</span><span class="p">(</span><span class="n">img1</span><span class="p">,</span> <span class="n">img2</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">avg_pool</span><span class="p">)</span>

        <span class="n">mcs</span> <span class="o">=</span> <span class="n">mcs</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">:</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">mcs_and_ssim</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">stack</span><span class="p">(</span><span class="n">mcs</span> <span class="o">+</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">relu</span><span class="p">(</span><span class="n">sim</span><span class="p">),))</span>
        <span class="n">mcs_and_ssim</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">pow</span><span class="p">(</span><span class="n">mcs_and_ssim</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">weight_tensor</span><span class="p">)</span>
        <span class="n">ms_ssim</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">prod</span><span class="p">(</span><span class="n">mcs_and_ssim</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>
        <span class="n">loss</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">reduce_mean</span><span class="p">(</span><span class="n">ms_ssim</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">loss</span></div>


<div class="viewcode-block" id="PSNR"><a class="viewcode-back" href="../../../../api_python/nn/mindspore.nn.PSNR.html#mindspore.nn.PSNR">[docs]</a><span class="k">class</span> <span class="nc">PSNR</span><span class="p">(</span><span class="n">Cell</span><span class="p">):</span>
    <span class="sa">r</span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Returns Peak Signal-to-Noise Ratio of two image batches.</span>

<span class="sd">    It produces a PSNR value for each image in batch.</span>
<span class="sd">    Assume inputs are :math:`I` and :math:`K`, both with shape :math:`h*w`.</span>
<span class="sd">    :math:`MAX` represents the dynamic range of pixel values.</span>

<span class="sd">    .. math::</span>

<span class="sd">        MSE&amp;=\frac{1}{hw}\sum\limits_{i=0}^{h-1}\sum\limits_{j=0}^{w-1}[I(i,j)-K(i,j)]^2\\</span>
<span class="sd">        PSNR&amp;=10*log_{10}(\frac{MAX^2}{MSE})</span>

<span class="sd">    Args:</span>
<span class="sd">        max_val (Union[int, float]): The dynamic range of the pixel values (255 for 8-bit grayscale images).</span>
<span class="sd">          The value must be greater than 0. Default: 1.0.</span>

<span class="sd">    Inputs:</span>
<span class="sd">        - **img1** (Tensor) - The first image batch with format &#39;NCHW&#39;. It must be the same shape and dtype as img2.</span>
<span class="sd">        - **img2** (Tensor) - The second image batch with format &#39;NCHW&#39;. It must be the same shape and dtype as img1.</span>

<span class="sd">    Outputs:</span>
<span class="sd">        Tensor, with dtype mindspore.float32. It is a 1-D tensor with shape N, where N is the batch num of img1.</span>

<span class="sd">    Raises:</span>
<span class="sd">        TypeError: If `max_val` is neither int nor float.</span>
<span class="sd">        ValueError: If `max_val` is less than or equal to 0.</span>
<span class="sd">        ValueError: If length of shape of `img1` or `img2` is not equal to 4.</span>

<span class="sd">    Supported Platforms:</span>
<span class="sd">        ``Ascend`` ``GPU`` ``CPU``</span>

<span class="sd">    Examples:</span>
<span class="sd">        &gt;&gt;&gt; net = nn.PSNR()</span>
<span class="sd">        &gt;&gt;&gt; img1 = Tensor([[[[1, 2, 3, 4], [1, 2, 3, 4]]]])</span>
<span class="sd">        &gt;&gt;&gt; img2 = Tensor([[[[3, 4, 5, 6], [3, 4, 5, 6]]]])</span>
<span class="sd">        &gt;&gt;&gt; output = net(img1, img2)</span>
<span class="sd">        &gt;&gt;&gt; print(output)</span>
<span class="sd">        [-6.0206]</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">max_val</span><span class="o">=</span><span class="mf">1.0</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">PSNR</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="n">validator</span><span class="o">.</span><span class="n">check_value_type</span><span class="p">(</span><span class="s1">&#39;max_val&#39;</span><span class="p">,</span> <span class="n">max_val</span><span class="p">,</span> <span class="p">[</span><span class="nb">int</span><span class="p">,</span> <span class="nb">float</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="p">)</span>
        <span class="n">validator</span><span class="o">.</span><span class="n">check_number</span><span class="p">(</span><span class="s1">&#39;max_val&#39;</span><span class="p">,</span> <span class="n">max_val</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="n">Rel</span><span class="o">.</span><span class="n">GT</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">max_val</span> <span class="o">=</span> <span class="n">max_val</span>

    <span class="k">def</span> <span class="nf">construct</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">img1</span><span class="p">,</span> <span class="n">img2</span><span class="p">):</span>
        <span class="n">_check_input_4d</span><span class="p">(</span><span class="n">F</span><span class="o">.</span><span class="n">shape</span><span class="p">(</span><span class="n">img1</span><span class="p">),</span> <span class="s2">&quot;img1&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="p">)</span>
        <span class="n">_check_input_4d</span><span class="p">(</span><span class="n">F</span><span class="o">.</span><span class="n">shape</span><span class="p">(</span><span class="n">img2</span><span class="p">),</span> <span class="s2">&quot;img2&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="p">)</span>
        <span class="n">P</span><span class="o">.</span><span class="n">SameTypeShape</span><span class="p">()(</span><span class="n">img1</span><span class="p">,</span> <span class="n">img2</span><span class="p">)</span>
        <span class="n">dtype_max_val</span> <span class="o">=</span> <span class="n">_get_dtype_max</span><span class="p">(</span><span class="n">F</span><span class="o">.</span><span class="n">dtype</span><span class="p">(</span><span class="n">img1</span><span class="p">))</span>
        <span class="n">max_val</span> <span class="o">=</span> <span class="n">F</span><span class="o">.</span><span class="n">scalar_cast</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">max_val</span><span class="p">,</span> <span class="n">F</span><span class="o">.</span><span class="n">dtype</span><span class="p">(</span><span class="n">img1</span><span class="p">))</span>
        <span class="n">max_val</span> <span class="o">=</span> <span class="n">_convert_img_dtype_to_float32</span><span class="p">(</span><span class="n">max_val</span><span class="p">,</span> <span class="n">dtype_max_val</span><span class="p">)</span>
        <span class="n">img1</span> <span class="o">=</span> <span class="n">_convert_img_dtype_to_float32</span><span class="p">(</span><span class="n">img1</span><span class="p">,</span> <span class="n">dtype_max_val</span><span class="p">)</span>
        <span class="n">img2</span> <span class="o">=</span> <span class="n">_convert_img_dtype_to_float32</span><span class="p">(</span><span class="n">img2</span><span class="p">,</span> <span class="n">dtype_max_val</span><span class="p">)</span>

        <span class="n">mse</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">ReduceMean</span><span class="p">()(</span><span class="n">F</span><span class="o">.</span><span class="n">square</span><span class="p">(</span><span class="n">img1</span> <span class="o">-</span> <span class="n">img2</span><span class="p">),</span> <span class="p">(</span><span class="o">-</span><span class="mi">3</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">))</span>
        <span class="n">psnr</span> <span class="o">=</span> <span class="mi">10</span> <span class="o">*</span> <span class="n">P</span><span class="o">.</span><span class="n">Log</span><span class="p">()(</span><span class="n">F</span><span class="o">.</span><span class="n">square</span><span class="p">(</span><span class="n">max_val</span><span class="p">)</span> <span class="o">/</span> <span class="n">mse</span><span class="p">)</span> <span class="o">/</span> <span class="n">F</span><span class="o">.</span><span class="n">scalar_log</span><span class="p">(</span><span class="mf">10.0</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">psnr</span></div>


<span class="nd">@constexpr</span>
<span class="k">def</span> <span class="nf">_raise_dims_rank_error</span><span class="p">(</span><span class="n">input_shape</span><span class="p">,</span> <span class="n">param_name</span><span class="p">,</span> <span class="n">func_name</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;raise error if input is not 3d or 4d&quot;&quot;&quot;</span>
    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">func_name</span><span class="si">}</span><span class="s2"> </span><span class="si">{</span><span class="n">param_name</span><span class="si">}</span><span class="s2"> should be 3d or 4d, but got shape </span><span class="si">{</span><span class="n">input_shape</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>


<span class="nd">@constexpr</span>
<span class="k">def</span> <span class="nf">_get_bbox</span><span class="p">(</span><span class="n">rank</span><span class="p">,</span> <span class="n">shape</span><span class="p">,</span> <span class="n">central_fraction</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;get bbox start and size for slice&quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">rank</span> <span class="o">==</span> <span class="mi">3</span><span class="p">:</span>
        <span class="n">c</span><span class="p">,</span> <span class="n">h</span><span class="p">,</span> <span class="n">w</span> <span class="o">=</span> <span class="n">shape</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">n</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">h</span><span class="p">,</span> <span class="n">w</span> <span class="o">=</span> <span class="n">shape</span>

    <span class="n">bbox_h_start</span> <span class="o">=</span> <span class="nb">int</span><span class="p">((</span><span class="nb">float</span><span class="p">(</span><span class="n">h</span><span class="p">)</span> <span class="o">-</span> <span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">(</span><span class="n">h</span> <span class="o">*</span> <span class="n">central_fraction</span><span class="p">))</span> <span class="o">/</span> <span class="mi">2</span><span class="p">)</span>
    <span class="n">bbox_w_start</span> <span class="o">=</span> <span class="nb">int</span><span class="p">((</span><span class="nb">float</span><span class="p">(</span><span class="n">w</span><span class="p">)</span> <span class="o">-</span> <span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">(</span><span class="n">w</span> <span class="o">*</span> <span class="n">central_fraction</span><span class="p">))</span> <span class="o">/</span> <span class="mi">2</span><span class="p">)</span>
    <span class="n">bbox_h_size</span> <span class="o">=</span> <span class="n">h</span> <span class="o">-</span> <span class="n">bbox_h_start</span> <span class="o">*</span> <span class="mi">2</span>
    <span class="n">bbox_w_size</span> <span class="o">=</span> <span class="n">w</span> <span class="o">-</span> <span class="n">bbox_w_start</span> <span class="o">*</span> <span class="mi">2</span>

    <span class="k">if</span> <span class="n">rank</span> <span class="o">==</span> <span class="mi">3</span><span class="p">:</span>
        <span class="n">bbox_begin</span> <span class="o">=</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">bbox_h_start</span><span class="p">,</span> <span class="n">bbox_w_start</span><span class="p">)</span>
        <span class="n">bbox_size</span> <span class="o">=</span> <span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">bbox_h_size</span><span class="p">,</span> <span class="n">bbox_w_size</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">bbox_begin</span> <span class="o">=</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">bbox_h_start</span><span class="p">,</span> <span class="n">bbox_w_start</span><span class="p">)</span>
        <span class="n">bbox_size</span> <span class="o">=</span> <span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">bbox_h_size</span><span class="p">,</span> <span class="n">bbox_w_size</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">bbox_begin</span><span class="p">,</span> <span class="n">bbox_size</span>


<div class="viewcode-block" id="CentralCrop"><a class="viewcode-back" href="../../../../api_python/nn/mindspore.nn.CentralCrop.html#mindspore.nn.CentralCrop">[docs]</a><span class="k">class</span> <span class="nc">CentralCrop</span><span class="p">(</span><span class="n">Cell</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Crops the central region of the images with the central_fraction.</span>

<span class="sd">    Args:</span>
<span class="sd">        central_fraction (float): Fraction of size to crop. It must be float and in range (0.0, 1.0].</span>

<span class="sd">    Inputs:</span>
<span class="sd">        - **image** (Tensor) - A 3-D tensor of shape [C, H, W], or a 4-D tensor of shape [N, C, H, W].</span>

<span class="sd">    Outputs:</span>
<span class="sd">        Tensor, 3-D or 4-D float tensor, according to the input.</span>

<span class="sd">    Raises:</span>
<span class="sd">        TypeError: If `central_fraction` is not a float.</span>
<span class="sd">        ValueError: If `central_fraction` is not in range (0, 1.0].</span>

<span class="sd">    Supported Platforms:</span>
<span class="sd">        ``Ascend`` ``GPU`` ``CPU``</span>

<span class="sd">    Examples:</span>
<span class="sd">        &gt;&gt;&gt; net = nn.CentralCrop(central_fraction=0.5)</span>
<span class="sd">        &gt;&gt;&gt; image = Tensor(np.random.random((4, 3, 4, 4)), mindspore.float32)</span>
<span class="sd">        &gt;&gt;&gt; output = net(image)</span>
<span class="sd">        &gt;&gt;&gt; print(output.shape)</span>
<span class="sd">        (4, 3, 2, 2)</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">central_fraction</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">CentralCrop</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="n">validator</span><span class="o">.</span><span class="n">check_value_type</span><span class="p">(</span><span class="s2">&quot;central_fraction&quot;</span><span class="p">,</span> <span class="n">central_fraction</span><span class="p">,</span> <span class="p">[</span><span class="nb">float</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="p">)</span>
        <span class="n">validator</span><span class="o">.</span><span class="n">check_float_range</span><span class="p">(</span><span class="n">central_fraction</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">,</span> <span class="n">Rel</span><span class="o">.</span><span class="n">INC_RIGHT</span><span class="p">,</span> <span class="s1">&#39;central_fraction&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">central_fraction</span> <span class="o">=</span> <span class="n">central_fraction</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">slice</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">Slice</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">construct</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">image</span><span class="p">):</span>
        <span class="n">image_shape</span> <span class="o">=</span> <span class="n">F</span><span class="o">.</span><span class="n">shape</span><span class="p">(</span><span class="n">image</span><span class="p">)</span>
        <span class="n">rank</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">image_shape</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">rank</span> <span class="ow">in</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">_raise_dims_rank_error</span><span class="p">(</span><span class="n">image_shape</span><span class="p">,</span> <span class="s2">&quot;image&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cls_name</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">central_fraction</span> <span class="o">==</span> <span class="mf">1.0</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">image</span>

        <span class="n">bbox_begin</span><span class="p">,</span> <span class="n">bbox_size</span> <span class="o">=</span> <span class="n">_get_bbox</span><span class="p">(</span><span class="n">rank</span><span class="p">,</span> <span class="n">image_shape</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">central_fraction</span><span class="p">)</span>
        <span class="n">image</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">slice</span><span class="p">(</span><span class="n">image</span><span class="p">,</span> <span class="n">bbox_begin</span><span class="p">,</span> <span class="n">bbox_size</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">image</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>

  <hr/>

  <div role="contentinfo">
    <p>
        &#169; Copyright 2021, MindSpore.

    </p>
  </div>
    
    
    
    Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>
        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>